import Vue from 'vue'
export default {
    namespaced: true, //namespced:true才能实现vuex的模块化
    state: {
        subjects: []
    },
    mutations: {
        getAllSubjectsMutations(state, payload) {
            state.subjects = payload
        }
    },
    actions: {
        async getAllSubjectsAsync(cxt) {
            console.log('-----我被组件的sa给调用了----------');
            const { data: { result } } = await Vue.prototype.$api.subjects.getSubjects()
            cxt.commit('getAllSubjectsMutations', result)
        },
        async addSubjectsAsync(cxt, subjects) {
            const { code } = await Vue.prototype.$api.subjects.addSubjects(subjects)
            const { data: { result } } = await Vue.prototype.$api.subjects.getSubjects()
            if (code) {
                cxt.commit('getAllSubjectsMutations', result)
            }
        }
    },
    getters: {
        showInfo(state) {
            return `共有${state.subjects.filter(item => item.courses.length > 0).length}个专业有课程,共有${state.subjects.filter(item => item.courses.length == 0).length}个专业没有课程`
        }
    }
}